package com.yangwei.examination.student.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yangwei.examination.student.pojo.AnswerPaper;
import com.yangwei.examination.student.service.AnswerPaperService;
import com.yangwei.examination.teacher.pojo.*;
import com.yangwei.examination.teacher.service.*;
import com.yangwei.examination.user.pojo.Student;
import com.yangwei.examination.user.service.TeacherService;
import com.yangwei.examination.utils.ResultUtils;
import com.yangwei.examination.utils.ResultVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;
@Api(tags = "学生获取测验信息",description = "学生-考试列表")
@RestController
@RequestMapping("/stu/Exam")
public class ExamStuController {

    @Resource
    private ExamService examService;

    @Resource
    private HandleClassService handleClassService;

    @Resource
    private PaperService paperService;

    @Resource
    private TeacherService teacherService;

    @Resource
    private PaperQuestionService paperQuestionService;

    @Resource
    private AnswerPaperService answerPaperService;


    @ApiOperation(value = "由学生信息查其名下所有测验(时间 是否答卷 发布人)")
    @PostMapping("/list")
    public ResultVo list(@ApiParam(name = "student", value = "班级 专业 院校id 年纪 非空") @RequestBody Student student) {

        System.out.println(student);
        //简便写法
        //查这个学生信息对应的接管班级
        LambdaQueryWrapper<HandleClass> handleClassLambdaQuery = Wrappers.lambdaQuery(HandleClass.class)
                .eq(HandleClass::getClassNum, student.getClassNum())
                .eq(HandleClass::getMajorName, student.getMajorName())
                .eq(HandleClass::getSchoolId, student.getSchoolId())
                .eq(HandleClass::getYear, student.getYear());

        List<HandleClass> list = handleClassService.list(handleClassLambdaQuery);

        //创建流 map 方法将 HandleClass 对象映射成对应 id collect 方法将映射后的结果收集到一个列表中
        List<Long> ids = list.stream()
                .map(HandleClass::getId)
                .collect(Collectors.toList());

        //查接管班级下的所有Exam 一个班级可能有多个老师接管 接管的老师可能发起了考试 可能没有发起
        LambdaQueryWrapper<Exam> examLambdaQuery= Wrappers.lambdaQuery(Exam.class)
                .in(Exam::getHandleClassId,ids)
                .orderByDesc(Exam::getUpdateTime);
        List<Exam> examList = examService.list(examLambdaQuery);

        //在exam中注入 Paper Teacher
        //Paper只注入PaperInfo
        for (Exam exam:examList){

            //注入Teacher
            exam.setTeacher(teacherService.getById(exam.getTeacherId()));

            //查询当前学生是否已经答卷 在AnswerPaper中查询是否存在对当前试卷的答卷
            AnswerPaper one = answerPaperService.getOne(
                    Wrappers.lambdaQuery(AnswerPaper.class)
                            .eq(AnswerPaper::getExamId, exam.getId())
                            .eq(AnswerPaper::getStudentId, student.getId())
            );
            if (one!=null){
                exam.setAnswerStatus(true);  //默认为false 答卷时设置为true
            }

            //已经关闭重复代码提示
            //将Paper注入PaperInfo
            Paper paper = paperService.getById(exam.getPaperId());
            PaperInfo paperInfo = new PaperInfo();
            for (int i = 1; i <= 4; i++) {
                QueryWrapper<PaperQuestion> paperQuestionQueryCount = new QueryWrapper<>();
                paperQuestionQueryCount.lambda().eq(PaperQuestion::getQuestionType, i).eq(PaperQuestion::getPaperId, paper.getId());
                if (i == 1)
                    paperInfo.setSingleNum(paperQuestionService.count(paperQuestionQueryCount));
                if (i == 2)
                    paperInfo.setMultipleNum(paperQuestionService.count(paperQuestionQueryCount));
                if (i == 3)
                    paperInfo.setFillInNum(paperQuestionService.count(paperQuestionQueryCount));
                if (i == 4)
                    paperInfo.setTextNum(paperQuestionService.count(paperQuestionQueryCount));
            }
            //注入PaperInfo
            paper.setPaperInfo(paperInfo);
            //最终注入Paper
            exam.setPaper(paper);
        }


        return ResultUtils.success("查询成功",examList);
    }

    //获取测验的数据信息 用于头部
    @ApiOperation(value = "获取测验的数据信息 用于头部")
    @GetMapping("/findById")
    public ResultVo findById(@ApiParam(name = "id",value = "examId唯一")@RequestParam Integer id){
        return ResultUtils.success("查询成功",examService.getById(id));
    }


}
